package com.zy.JSON;

import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/jsonTestTwo")
public class JsonTestTwo extends HttpServlet
{
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
    {
        //设置响应数据格式为json
        resp.setContentType("application/json;charset=UTF-8");

        String username = req.getParameter("username");
        Map<String,Object> map=new HashMap<String,Object>();
        if("Tom".equals(username))
        {
            map.put("user_exist",true);
            map.put("msg","名字被占用了，请重新输入！！");
        }
        else
        {
            map.put("user_exist",false);
            map.put("msg","用户名可用！");
        }
        ObjectMapper objectMapper=new ObjectMapper();
        objectMapper.writeValue(resp.getWriter(),map);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
    {
        this.doPost(req,resp);
    }
}
